home *** CD-ROM | disk | FTP | other *** search
/ Hyper Stacks 1994 May / Hyper Stacks (Pacific HiTech)(1994)[Mac].iso / XCMDS&XFCNS / PhoneDecode / xfcn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-23  |  3.4 KB  |  114 lines  |  [TEXT/KAHL]

  1. /* Copyright ©November 1993, Dale Schaafsma  All rights reserved */
  2. /* This is an xfcn for hyperdard which does the following:
  3. /*      opens the microphone, waits until a "loud" sound, takes the next 1024 bytes
  4. /*      runs a fast fourier trasform on the data, and calls if appropriate the script
  5. /*      for the key hit on a touch tone phone */
  6. /************************************************************/
  7. /* many thanks to:
  8. /*     the author of Sound Confusion (Copyright © 1992 Bernie Bernstein ) the source 
  9. /*         for the microphone
  10. /*     Numerical recipies for the fft algorithm which finally worked
  11. /*     and finally many authors of xcmd/xfcns who submitted their source for the
  12. /*         hypercard interface of things */
  13.  
  14. #include <types.h>
  15. #include <sound.h>
  16. #include <soundinput.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <Memory.h>
  20. #include <HyperXcmd.h>
  21. #include <SetUpA4.h>
  22. #include <pascal.h>
  23.  
  24. #include "record.h"
  25. Boolean phInitialized = false;
  26. Boolean error = false;
  27. Boolean mylock = false;
  28. StringPtr *phScripts[12];
  29. #define kUsageString "\pUsage:PhoneDecode <script0> <script1> .. <script9> <script*> <script#>"
  30.  
  31. pascal void main(XCmdPtr paramPtr)
  32. {
  33. int err;
  34. long i=0,j=0,num=0;
  35. RememberA0();
  36. SetUpA4();
  37. /* catch error, initialized or exit */
  38. /* if exit, and initialized */
  39. if ((((**(paramPtr -> params[0])) == 'q') || ((**(paramPtr -> params[0])) == 'Q')) && phInitialized)  {
  40.     finishToneRecognize();
  41.     paramPtr->returnValue = PasToZero(paramPtr,"\pDeallocated");
  42.     phInitialized = false;
  43.     goto out;
  44.     }
  45. /* if error */
  46. if (error) {
  47.     paramPtr->returnValue = PasToZero(paramPtr,"\pPhoneDecode: error occurred!");
  48.     goto out;
  49.     }
  50. /* if initialized use this as idle time */
  51. if (phInitialized && !mylock) {
  52.     mylock = true;
  53.     if ((err = toneRecognize(paramPtr)) != kNoError)
  54.         paramPtr->returnValue = PasToZero(paramPtr,"\pError in tone");
  55.     mylock = false;
  56.     goto out;
  57.     }
  58. else if (phInitialized && mylock) goto out;
  59.  
  60. /* if not enough args pass back the usage or other info */
  61. if (paramPtr -> paramCount < 12) {
  62.     if ((**(paramPtr -> params[0])) == '!') {
  63.         paramPtr->returnValue = PasToZero(paramPtr,"\pThis xcmd should have accompanying docs\n\
  64. Written by Dale Schaafsma, FREEware!");
  65.         goto out;
  66.         }
  67.     else {
  68.         paramPtr->returnValue = PasToZero(paramPtr,kUsageString);
  69.         goto out;
  70.         }
  71.     }
  72.     
  73. /* start initializing */
  74. else {
  75.     if ((err = initToneRecognize()) != kNoError) {
  76.         switch (err) {
  77.             case kNoInput:
  78.                 paramPtr->returnValue = PasToZero(paramPtr,"\pNo Input");
  79.                 break;
  80.             case kGestaltFailed:
  81.                 paramPtr->returnValue = PasToZero(paramPtr,"\pGestalt Failed");
  82.                 break;
  83.             case kOpeningDevice:
  84.                 paramPtr->returnValue = PasToZero(paramPtr,"\pOpen Device Failed");
  85.                 break;
  86.             case kGettingRate:
  87.                 paramPtr->returnValue = PasToZero(paramPtr,"\pError Getting Rate");
  88.                 break;
  89.             case kMemory:
  90.                 paramPtr->returnValue = PasToZero(paramPtr,"\pMemory Error");
  91.                 break;
  92.             case kBufSetup:
  93.                 paramPtr->returnValue = PasToZero(paramPtr,"\pError Setting Up Buffers");
  94.                 break;
  95.             default:
  96.                 paramPtr->returnValue = PasToZero(paramPtr,"\pError Initializing ToneRecognize");
  97.                 break;
  98.             }
  99.         error = true;
  100.         goto out;
  101.         }
  102.     /* actually grab the script names the user wants to use */
  103.     for(i=0; i<12; i++) {
  104.         phScripts[i] = (StringPtr) NewHandle(strlen(*(paramPtr -> params[i])));
  105.         strcpy(*(phScripts[i]), (*(paramPtr -> params[i])));
  106.         }
  107.  
  108.     paramPtr->returnValue = PasToZero(paramPtr,"\pInitialized");
  109.     phInitialized = true;
  110.     goto out;
  111.     }
  112. out:
  113.     RestoreA4();return;
  114. }